package com.linghang.wusthelper.spider.jwc;

import com.linghang.wusthelper.redis.RedisService;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author origin
 * 课程操作
 */
@Component
public class CourseOpration {

    private static Logger log = LoggerFactory.getLogger(JwcSimLogin.class);

    private static PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();

    private static RedisService redisService;

    static {
        connectionManager.setMaxTotal(100);
    }

    @Autowired
    private void setRedisService(RedisService redisService){
        CourseOpration.redisService = redisService;
    }

    /**
     * 选课
     * @param JSESSIONID
     * @param SERVERID
     * @param url
     * @return
     * @throws IOException
     */
    public static String selectCourse(String JSESSIONID,String SERVERID,String url) throws IOException {

        HttpGet httpGet = new HttpGet("http://jwxt.wust.edu.cn"+url);
        CookieStore cookieStore = new BasicCookieStore();

        //  添加cookie
        BasicClientCookie cookie = new BasicClientCookie("JSESSIONID",JSESSIONID);
        cookie.setDomain("jwxt.wust.edu.cn");
        cookie.setPath("/");
        cookie.setVersion(0);
        cookieStore.addCookie(cookie);
        BasicClientCookie cookie2 = new BasicClientCookie("SERVERID",SERVERID);
        cookie.setDomain("jwxt.wust.edu.cn");
        cookie.setPath("/");
        cookie.setVersion(0);
        cookieStore.addCookie(cookie2);
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultCookieStore(cookieStore).build();

        //  设置请求头
        httpGet.setHeader("Connection", "keep-alive");
        httpGet.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        httpGet.setHeader("Accept-Encoding", "gzip,deflate,sdch");
        httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36");

        HttpResponse httpResponse = httpClient.execute(httpGet);
        //todo


        return null;
    }

    /**
     * 退选课程
     * @param JSESSIONID
     * @param SERVERID
     * @param url
     * @return
     * @throws IOException
     */
    public static String withdrawCourse(String JSESSIONID,String SERVERID,String url) throws IOException {
        HttpGet httpGet = new HttpGet("http://jwxt.wust.edu.cn"+url);
        CookieStore cookieStore = new BasicCookieStore();

        //  添加cookie
        BasicClientCookie cookie = new BasicClientCookie("JSESSIONID",JSESSIONID);
        cookie.setDomain("jwxt.wust.edu.cn");
        cookie.setPath("/");
        cookie.setVersion(0);
        cookieStore.addCookie(cookie);
        BasicClientCookie cookie2 = new BasicClientCookie("SERVERID",SERVERID);
        cookie.setDomain("jwxt.wust.edu.cn");
        cookie.setPath("/");
        cookie.setVersion(0);
        cookieStore.addCookie(cookie2);
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultCookieStore(cookieStore).build();

        //  设置请求头
        httpGet.setHeader("Connection", "keep-alive");
        httpGet.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        httpGet.setHeader("Accept-Encoding", "gzip,deflate,sdch");
        httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36");

        HttpResponse httpResponse = httpClient.execute(httpGet);
        //todo

        return null;
    }
}
